//Will Zepeda
//GSP-362
//SpriteInfo.cpp
//class that retains info for a sprite
#include "SpriteInfo.h"
#include "DirectX.h"
#include <string>

SpriteInfo::~SpriteInfo()
{
	if( NULL != mpTexture )
	{
		mpTexture->Release();
	}
}
void SpriteInfo::loadTexture(char * aFileName)
{
	//manipulating argument FileName to add Folders then using
	//string.c_str() function to return string as char *
	std::string sFile("./Assets/Sprite_Textures/" + std::string(aFileName));
	//creating texture from file, DUH
	HRESULT hr = D3DXCreateTextureFromFileEx(DirectXFramework::Instance()->getDevice(), 
		sFile.c_str(), 
		D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2, 0, 0, 
		D3DFMT_UNKNOWN, D3DPOOL_MANAGED, 
		D3DX_DEFAULT, D3DX_DEFAULT, 
		D3DCOLOR_XRGB(255, 0, 255), 
		&mImageInfo, 0, &mpTexture);

	//Set the sprites initial color
	mColor = D3DCOLOR_ARGB(255, 255, 255, 255);

	//if (FAILED(hr))
	//{
	//	//print out error in message box
	//	::MessageBox(::HWND(), "D3DXCreateTextureFromFile() failed in SpriteInfo.cpp", "Mayan Maze", 0);
	//	//delete the device pointer
	//	if (NULL != mpTexture)
	//	{
	//		mpTexture->Release();
	//	}
	//	if (NULL != DirectXFramework::Instance()->getDevice())
	//	{
	//		DirectXFramework::Instance()->getDevice()->Release();
	//	}
	//	//exit the process because initialization failed, duh
	//	ExitProcess(-1);
	//}
	//set sprite info's center based on loaded texture's width and height.
	mWidth = mImageInfo.Width;
	mHeight = mImageInfo.Height;
	setCenter();
}

void SpriteInfo::setCenter()
{
	mCenter.X = mWidth/2;
	mCenter.Y = mHeight/2;
}
void SpriteInfo::setPosOfCenter(POINT mouse)
{
	mPosOfCenter.X = mouse.x;
	mPosOfCenter.Y = mouse.y;
}
void SpriteInfo::setPosOfCenter(float aX, float aY)
{
	mPosOfCenter.X = (int)aX;
	mPosOfCenter.Y = (int)aY;
}
void SpriteInfo::setPosOfCenter(int aX, int aY)
{
	mPosOfCenter.X = aX;
	mPosOfCenter.Y = aY;
}
void SpriteInfo::setWidth(int aWidth)
{
	mWidth = aWidth;
}
void SpriteInfo::setHeight(int aHeight)
{
	mHeight = aHeight;
}
void SpriteInfo::setScale(float aScale)
{
	mScale = mScaleY = aScale;
}
void SpriteInfo::setScale(float xScale, float yScale)
{
	mScale = xScale;
	mScaleY = yScale;
}
void SpriteInfo::setColor(int A, int R, int G, int B)
{
	mColor = D3DCOLOR_ARGB(A, R, G, B);
}
bool SpriteInfo::inSprite(POINT coord)
{
	//check to see if mouse click is in sprite
	if(coord.x <= mPosOfCenter.X + (int)((mWidth*mScale)/2) &&
	   coord.x >= mPosOfCenter.X - (int)((mWidth*mScale)/2))
	{
		if(coord.y <= mPosOfCenter.Y + (int)((mHeight*mScale)/2) &&
		   coord.y >= mPosOfCenter.Y - (int)((mHeight*mScale)/2))
		{
			return true;
		}
	}
	return false;
}
bool SpriteInfo::inSprite(XY coord)
{
	//check to see if mouse click is in sprite
	if(coord.X <= mPosOfCenter.X + (int)((mWidth*mScale)/2) &&
	   coord.X >= mPosOfCenter.X - (int)((mWidth*mScale)/2))
	{
		if(coord.Y <= mPosOfCenter.Y + (int)((mHeight*mScale)/2) &&
		   coord.Y >= mPosOfCenter.Y - (int)((mHeight*mScale)/2))
		{
			return true;
		}
	}
	return false;
}